﻿using System;
using System.Collections;
using System.Collections.Generic;

using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;
using EZCRS.HCMS.Web;

namespace EZCRS.HCMS.Web.manage.hotel
{
    public partial class planlist : ManageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            if (!IsPostBack)
            {
                CheckModule(65);

                InitPage();

                if (Request.QueryString["action"] != null)
                {
                    //get data
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        int iPageIndex = 1;
                        int iPageSize = 15;

                        int iState = 1;
                        string strRoom = string.Empty;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        if (!string.IsNullOrEmpty(Request.Params["query"]))
                            ParseQuery(Request.Params["query"], out strRoom, out iState);

                        Response.Write(GetJSON(iPageIndex, iState, strRoom, iPageSize));
                        Response.End();
                    }
                }
            }
        }

        /// <summary>
        /// 解析查询
        /// </summary>
        /// <param name="Query"></param>
        /// <param name="Room"></param>
        /// <param name="State"></param>
        private void ParseQuery(string Query, out string Room, out int State)
        {
            string[] sp = Query.Split(',');

            State = -1;
            Room = string.Empty;

            if (sp.Length == 2)
            {
                Room = sp[0];
                int.TryParse(sp[1], out State);
            }
        }


        private void InitPage()
        {
            Bllh_roomcode bl = new Bllh_roomcode();

            string strLan = base.LanguageId.ToString();

            List<HCRS_RoomCode> lst = bl.getitem("pid = " + base.BasPropertyId + " AND avaliable = 1");

            foreach (HCRS_RoomCode room in lst)
                ddlRoomType.Items.Add(new ListItem(room.roomname, room.roomcode));

            ddlRoomType.Items.Insert(0, new ListItem(Resources.Site.DplistPleaseSelect, ""));

            //rblStatus.SelectedIndex = 0;
        }

        /// <summary>
        /// 获取JSON数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="State"></param>
        /// <param name="Room"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        private string GetJSON(int PageIndex, int State, string Room, int PageSize)
        {
            BllRatePlan bllPlan = new BllRatePlan();

            int iRowCount = 0;

            int iLan =  base.LanguageId;

            List<HCRS_Plan> lstPlan =
                bllPlan.getListByPage(base.BasPropertyId, State, iLan, Room, PageIndex, PageSize, out iRowCount);

            string strJson = "{\"total\":" + iRowCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

            foreach (HCRS_Plan plan in lstPlan)
            {
                strJson += "{\"id\":" + plan.ID.ToString() + ",";
                strJson += "\"cell\":[";

                strJson += "\"" + HttpUtility.HtmlEncode(plan.PlanCode) + "\",";
                strJson += "\"" + HttpUtility.HtmlEncode(plan.PlanName) + "\",";

                if (!string.IsNullOrEmpty(plan.PlanDesc))
                {
                    if (plan.PlanDesc.Length > 80)
                        plan.PlanDesc = plan.PlanDesc.Substring(0, 80) + "...";

                    strJson += "\"" + HttpUtility.HtmlEncode(plan.PlanDesc) + "\",";
                }
                else
                    strJson += "\"无\",";

                if(plan.available == 1)
                    strJson += "\""+Resources.Site.LabActive+"\"";
                else
                    strJson += "\""+Resources.Site.LabNoActive+"\"";

                strJson += "]},";
            }

            strJson = strJson.Replace("\r\n", " ");

            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }
    }
}
